/**
 * https://leetcode.cn/problems/reshape-the-matrix/
 */
function matrixReshape(mat: number[][], r: number, c: number): number[][] {
  const row = mat.length;
  const col = mat[0].length;
  if (row * col != r * c) {
    return mat;
  }

  const ans = new Array(r).fill(0).map(() => new Array(c).fill(0));
  for (let x = 0; x < row * col; ++x) {
    // 关键是找到对应关系
    ans[Math.floor(x / c)][x % c] = mat[Math.floor(x / col)][x % col];
  }
  return ans;
}
